<?php
namespace app\tripapi\model;
use think\Model;
use think\Db;
class TravelPlanItem extends Model{
	/*
	 * 保存
	 */
	public function TravelPlanItemSave($id, $params){
	    $time = time();
        $number = $_POST['number1']; // 传递添加的条目 依据单一条目的数据下标key值进行其他参数的传递 TODO： 新条目是否为空 暂未做判断处理
        foreach($number as $key => $value){
            // 依据传递的参数值 转换travel_plan_item 数组明细
            $output['plan_id'] = $id;
            $output['department1'] = iconv('UTF-8', 'gbk', $params['department1'][$key]); // 一级部门
            $output['department2'] = iconv('UTF-8', 'gbk', $params['department2'][$key]); // 二级部门
            $output['report_user'] = iconv('UTF-8', 'gbk', $params['report_user'][$key]); // 填报人
            $output['contract_num'] = $params['contract_num'][$key]; // 项目/合同编号
            $output['traveler'] = iconv('UTF-8', 'gbk', $params['traveler'][$key]); // 出差人
            $output['traveler_num'] = $params['traveler_num'][$key]; // 出差人数
            $output['start_time'] = $params['start_time'][$key]; // 开始时间 - 时间戳转换
            $output['end_time'] = $params['end_time'][$key]; // 结束时间 - 时间戳转换
            $output['day_num'] = $params['day_num'][$key]; // 出差天数
            $output['trip_address'] = iconv('UTF-8', 'gbk', $params['trip_address'][$key]); // 出差地
            $output['customer_name'] = iconv('UTF-8', 'gbk', $params['customer_name'][$key]); // 客户名称
            $output['reason'] = iconv('UTF-8', 'gbk', $params['reason'][$key]); // 出差事由
            $output['traffic_money'] = $params['traffic_money'][$key]; // 交通费
            $output['hotel_money'] = $params['hotel_money'][$key]; // 住宿费
            $output['meal_money'] = $params['meal_money'][$key]; // 餐费
            $output['total_money'] = $params['total_money'][$key]; // 合计
            $output['remark'] = iconv('UTF-8', 'gbk', $params['remark'][$key]); // 备注
            $output['travel_status'] = 'publish'; // 修改状态 - 用于保留修改历史
            $output['creator'] = iconv('UTF-8', 'gbk', $params['creator']);; // 创建人
            $output['creator_id'] = $params['creator_id']; // 创建人id
            $output['create_time'] = $time; // 创建时间
            //存储 travel plan Item 明细表
            Db::name('travel_plan_item')->insert($output);
        }
//        return true;
    }

    /*
     * 详情列表 、 单一
     */
    public function GetList($id){
        $result = Db::name('travel_plan_item')->where([
            'plan_id'=>['=',$id],
            'travel_status'=>['=','publish']
        ])->select();
        foreach($result as $key => $value){
            $result[$key]['department1'] = iconv('GBK', 'UTF-8', $value['department1']);
            $result[$key]['department2'] = iconv('GBK', 'UTF-8', $value['department2']);
            $result[$key]['report_user'] = iconv('GBK', 'UTF-8', $value['report_user']);
            $result[$key]['traveler'] = iconv('GBK', 'UTF-8', $value['traveler']);
            $result[$key]['trip_address'] = iconv('GBK', 'UTF-8', $value['trip_address']);
            $result[$key]['customer_name'] = iconv('GBK', 'UTF-8', $value['customer_name']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['remark'] = iconv('GBK', 'UTF-8', $value['remark']);
        }
        return $result;
    }
    /*
     * 更新 - 保留原数据 用作历史数据
     */
    public function TravelPlanItemUpdate($params){
	    $data['travel_status'] = "modify";
	    Db::name('travel_plan_item')->where([
            'plan_id'=>['=',$params['planid']],
            'travel_status'=>['=','publish']
        ])->update($data);
        $time = time();
        $number = $params['number1']; // 传递添加的条目 依据单一条目的数据下标key值进行其他参数的传递 TODO： 新条目是否为空 暂未做判断处理
        foreach($number as $key => $value){
            // 依据传递的参数值 转换travel_plan_item 数组明细
            $output['plan_id'] = $params['planid'];
            $output['department1'] = iconv('UTF-8', 'gbk', $params['department1'][$key]); // 一级部门
            $output['department2'] = iconv('UTF-8', 'gbk', $params['department2'][$key]); // 二级部门
            $output['report_user'] = iconv('UTF-8', 'gbk', $params['report_user'][$key]); // 填报人
            $output['contract_num'] = $params['contract_num'][$key]; // 项目/合同编号
            $output['traveler'] = iconv('UTF-8', 'gbk', $params['traveler'][$key]); // 出差人
            $output['traveler_num'] = $params['traveler_num'][$key]; // 出差人数
            $output['start_time'] = $params['start_time'][$key]; // 开始时间 - 时间戳转换
            $output['end_time'] = $params['end_time'][$key]; // 结束时间 - 时间戳转换
            $output['day_num'] = $params['day_num'][$key]; // 出差天数
            $output['trip_address'] = iconv('UTF-8', 'gbk', $params['trip_address'][$key]); // 出差地
            $output['customer_name'] = iconv('UTF-8', 'gbk', $params['customer_name'][$key]); // 客户名称
            $output['reason'] = iconv('UTF-8', 'gbk', $params['reason'][$key]); // 出差事由
            $output['traffic_money'] = $params['traffic_money'][$key]; // 交通费
            $output['hotel_money'] = $params['hotel_money'][$key]; // 住宿费
            $output['meal_money'] = $params['meal_money'][$key]; // 餐费
            $output['total_money'] = $params['total_money'][$key]; // 合计
            $output['remark'] = iconv('UTF-8', 'gbk', $params['remark'][$key]); // 备注
            $output['travel_status'] = 'publish'; // 修改状态 - 用于保留修改历史
            $output['creator'] = iconv('UTF-8', 'gbk', $params['creator']);; // 创建人
            $output['creator_id'] = $params['creator_id']; // 创建人id
            $output['create_time'] = $time; // 创建时间
            //存储 travel plan Item 明细表
            Db::name('travel_plan_item')->insert($output);
        }
//        return true;
    }
    /*
     * 删除
     */
    public function PlanItemDelete($id){
        $result = Db::name('travel_plan_item')->where('plan_id', '=', $id)->delete();
        return $result;
    }

    public function GetItemList($data){
        $result = Db::name('travel_plan_item')->select();
        $count = count($result);
        $current_page = $data['page'];
        $startSize = "10";
        $lastSize = $startSize*($current_page - 1);
        $res = Db::name('travel_plan_item')->limit($lastSize, $startSize)->select();
        foreach($res as $key => $value){
            $res[$key]['count'] = $count;
        }
        return $res;
    }

}

?>